/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.cashmate.servlet;

import com.cashmate.hibernate.dao.LoansDAO;
import com.cashmate.hibernate.pojo.Loans;
import com.cashmate.utilities.EnglishNumberToWords;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author DELL
 */
public class PrintChecks extends HttpServlet {
   
         @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        LoansDAO loanDAO = (LoansDAO) request.getServletContext().getAttribute("loansDAO");
        response.setContentType("application/pdf");
        SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
        NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.ROOT);
        String selected = request.getParameter("selected");
        String loans[] = selected.split(",");
        try{
        Document doc = new Document();
        float f1 = 576;
        float f2 = 216;
        float margintop = 36;
        float marginleft = 56;
        float marginright = 18;
        Rectangle r = new Rectangle(f1, f2);       
        doc.setPageSize(r);
        doc.setMargins(marginleft, marginright, margintop, 0);
        
        PdfWriter writer = PdfWriter.getInstance(doc, response.getOutputStream());
        writer.addViewerPreference(PdfName.PRINTSCALING, PdfName.NONE);       

        doc.open();
        
        for(String l : loans){
            Loans loan = new Loans();
            int loanid = 0;
            try{
                loan = loanDAO.get(loanid);
            }catch(Exception e){
                
            }
            
            //replaceables
                String name = loan.getBorrowers().getCompletename();
                String date = format.format(new Date());
                
                double amount = loan.getNetloanableamount();
            //
        
            float[] colsWidth = {3f, 1f};
            PdfPTable ppt = new PdfPTable(colsWidth);
            ppt.setWidthPercentage(100);
            ppt.getDefaultCell().setBorder(Rectangle.NO_BORDER);
            ppt.getDefaultCell().setFixedHeight(22);

            Font fontInfo = new Font(FontFamily.HELVETICA, 14);
            Font fontAmt = new Font(FontFamily.HELVETICA, 14, Font.BOLD);

            Paragraph par = new Paragraph(date, fontInfo);
            Paragraph par5 = new Paragraph("");          
            
            Paragraph par2 = new Paragraph("");
            
            try{
                String dec = nf.format(amount);
                String point = dec.substring(dec.length() - 2, dec.length());
                
                int decimal = Integer.parseInt(point);                
                if(decimal > 0){
                    par2 = new Paragraph("**"+EnglishNumberToWords.convert((int)amount) +" & " +  decimal + "/100" +"**", fontAmt);
                }else{
                    par2 = new Paragraph("**"+EnglishNumberToWords.convert((int)amount) +"**", fontAmt);
                }
                
            }catch(Exception e){                    
                e.printStackTrace();
                par2 = new Paragraph("**"+EnglishNumberToWords.convert((int)amount) +"**", fontAmt);
            }


            PdfPCell ppc2 = new PdfPCell(par2);
    //        ppc2.setIndent(36);
            ppc2.setColspan(2);
            ppc2.setBorder(Rectangle.NO_BORDER);
            ppc2.setRowspan(2);
            ppc2.setFollowingIndent(36);
            ppc2.setPaddingTop(6);


            Paragraph par3 = new Paragraph("**"+ name +"**", fontInfo);
            Paragraph par4 = new Paragraph("**"+ nf.format(amount).substring(2, nf.format(amount).length()) +"**", fontInfo);

            PdfPCell ppc = new PdfPCell(par3);
            ppc.setIndent(36);
            ppc.setFixedHeight(22);
            ppc.setBorder(Rectangle.NO_BORDER);

            ppt.addCell(par5);
            ppt.addCell(par);

            ppt.addCell(ppc);

            ppc = new PdfPCell(par4);
            ppc.setFixedHeight(22);
            ppc.setBorder(Rectangle.NO_BORDER);

            ppt.addCell(ppc);
            ppt.addCell(ppc2);


            doc.add(ppt); 
            doc.newPage();
        }
        
        doc.close();
        }catch(Exception e){
            e.printStackTrace();
        }


    }
}
